
(function($) {
  //
  // plugin definition
  //
	
	
  
  $.fn.txtToSwf = function(options) {

	// build main options before element iteration
	var opts = $.extend({}, $.fn.txtToSwf.defaults, options);
	
	//check if flash is equal or greater to the required flash version.
	if (hasFlash($.fn.txtToSwf.defaults.version) == true) {
		
		//get a random perfix for the id sets;
		var rand_prefix = getRandomPrefix();
		// iterate and reformat each matched element
		return this.each(function(i){
			
			$this = $(this);
			
			var txt = cleanUptext($this.html());
			var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
			var swfToTextId = rand_prefix+"_swfToTxt"+i;
			var spanId = rand_prefix+"_spanID"+i;
			var $linkTemp = $this.children('a');
			var $spanObj = $('<span class="swfToTxt"></span>');
			var $spanObj2 = $('<span id="'+spanId+'"></span>');
			var flashLoc = o.swfLocation + o.swf;
			var aLink;
			var aHover = o.aHover ? o.aHover : undefined;
			var underline = o.underline != undefined ? o.underline :  $linkTemp.css("text-decoration");
			var color = o.color ? o.color : $.browser.opera ? $this.css('color') : RGBToHex($this.css('color'));
			var fontweight = o.fontweight ? o.fontweight : $this.css('font-weight');
			var italic = o.italic ? o.italic : $this.css('font-style');
			var fontSize = o.fontSize ? o.fontSize : $this.css('font-size');
			var lineHeight = o.lineHeight ? o.lineHeight : 2;
			var letterSpacing = o.letterSpacing ? o.letterSpacing : 0;
			var trigger = o.trigger;
			var w = o.width ? o.width : $(this).outerWidth();
			var h = o.height ? o.height : $(this).outerHeight();
			var x = o.x ? o.x : 0;
			var y = o.y ? o.y : 0;
			
			var txtToSwf_id = generatetxtToSwId();
			
			
			//check for links
			if($linkTemp.size() > 0){
				aLink = $.browser.opera ? $linkTemp.css('color') : RGBToHex($linkTemp.css('color'));
	
				if(underline == true){
					underline = "underline";
				}else if(underline == false){
					underline = 'none';
				}
				
			}
			
			
			if($.browser.msie && fontweight == 700 || $.browser.opera && fontweight == 700 ){
				fontweight = "bold";
			}
			
		
			//check uppcase styling
			if($this.css('text-transform') == "uppercase"){
				txt = txt.toUpperCase();
			}
			//setup isTrigger
			var isTrigger = false;
			
			if (typeof trigger == "string") {
				isTrigger = true;
			}else{
				isTrigger = false;
			}
			
			
			$this.wrapInner($spanObj);
			
			$this.children('span.swfToTxt').css('display', 'none');
			if($.browser.msie){
				$spanObj2.appendTo($this);
				var elementId = $spanObj2.attr('id');
				var flaObj = '<OBJECT id="'+swfToTextId+'" height="'+h+'" width="'+w+'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"><PARAM value="'+flashLoc+'" name="movie" /><PARAM value="#000000" name="bgcolor" /><PARAM value="high" name="quality" /><PARAM value="always" name="AllowScriptAccess" /><PARAM value="transparent" name="wmode" /><PARAM value="txt='+txt+'&txtColor='+color+'&fontWeight='+fontweight+'&fontSize='+fontSize+'&w='+w+'&h='+h+'&x='+x+'&y='+y+'&lineHeight='+lineHeight+'&letterSpacing='+letterSpacing+'&aLink='+aLink+'&aHover='+aHover+'&underline='+underline+'&italic='+italic+'&isTrigger='+isTrigger+'&soid='+swfToTextId+'&txtToSwf_id='+txtToSwf_id+'" name="flashvars" /></OBJECT> ';
				var n = (typeof elementId == 'string') ? document.getElementById(elementId) : elementId;
				n.innerHTML = flaObj;
				var $flaObj = $spanObj2.children();
			
			}else{
				var $flaObj = $('<embed height="'+h+'" width="'+w+'" wmode="transparent" quality="high" bgcolor="#000000" name="'+swfToTextId+'" id="'+swfToTextId+'" flashvars="txt='+txt+'&txtColor='+color+'&fontWeight='+fontweight+'&fontSize='+fontSize+'&w='+w+'&h='+h+'&x='+x+'&y='+y+'&lineHeight='+lineHeight+'&letterSpacing='+letterSpacing+'&aLink='+aLink+'&aHover='+aHover+'&underline='+underline+'&italic='+italic+'&isTrigger='+isTrigger+'&soid='+swfToTextId+'&txtToSwf_id='+txtToSwf_id+'"  src="'+flashLoc+'" AllowScriptAccess="always" type="application/x-shockwave-flash"/>');
				$flaObj.appendTo($this);
			}
			
			
			
			
			if (isTrigger) {
			
				$this.bind(trigger, function(event, txt){
				
					var txtFormat = txt.replace(/\"/g, "%22");
					if ($.browser.msie) {
						try {
							var id = $(this).children('span').children('object').attr('id');
						}catch(e){
							
						}
						
					}
					else {
						
						try {
							var id = $(this).children('embed').attr('id');
						}catch(e){
							
						}
					}
					
					try {
						var swfObj = document.getElementById(id);
					}catch(e){
						
					}
					try {
						swfObj.newText(txtFormat);
					}catch(e){
						
					}
						
					
				});
			}
			
		});
	}
  };
  
$.fn.txtToSwf.text = function(options){
		var opts = $.extend({}, $.fn.txtToSwf.defaults, options);
		this.each(function(){
		//debug('hihihi');
		});
		
		
}
  
  //
  // private function for debugging
  //
  function debug($obj) {
	//if (window.console && window.console.log)
	  //window.console.log( $obj);
  };
  //
  // define and expose our format function
  //
  function hasFlash() {
	// look for a flag in the query string to bypass flash detection
	if(/hasFlash\=true/.test(location)) return true;
	if(/hasFlash\=false/.test(location)) return false;
	var pv = playerVersion().match(/\d+/g);
	var rv = String([arguments[0], arguments[1], arguments[2]]).match(/\d+/g) || String($$.pluginOptions.version).match(/\d+/g);
	for(var i = 0; i < 3; i++) {
		pv[i] = parseInt(pv[i] || 0);
		rv[i] = parseInt(rv[i] || 0);
		// player is less than required
		
		if(pv[i] < rv[i])  return false;
		// player is greater than required
		
		if(pv[i] > rv[i]) return true;
	}
	// major version, minor version and revision match exactly
	debug("major version, minor version and revision match exactly");
	return true;
};

 function playerVersion() {
	// ie
	try {
		try {
			// avoid fp6 minor version lookup issues
			// see: http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
			var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
			try { axo.AllowScriptAccess = 'always';	} 
			catch(e) { return '6,0,0'; }				
		} catch(e) {}
		return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1];
	// other browsers
	} catch(e) {
		try {
			if(navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin){
				try {
					return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1];
				}catch(e){}
			}
		} catch(e) {}		
	}
	return '0,0,0';
};



var tohex = new Array;
for(var i = 0; i < 10; i++) {
	tohex[i] = i;
}
tohex[10] = "A"; 
tohex[11] = "B";
tohex[12] = "C";
tohex[13] = "D";
tohex[14] = "E";
tohex[15] = "F";

function RGBToHex(rgb) {
	
	if($.browser.msie)
		return rgb;
	var rgbIndex = rgb.indexOf("(");
	if (rgbIndex > -1) {
		var rgb1 = rgb.substring(rgbIndex+1,rgb.length);
		var rgbIndex2 = (rgb1.indexOf(")"));
		var rgb2 = rgb1.substr(0,rgbIndex2);
	}
	var temp = rgb2.split(",");
	var hr = hex(temp[0]);
	var hg = hex(temp[1]);
	var hb = hex(temp[2]);
	
	return "#" + hr + hg + hb;
}

function hex(i) {
	if (i < 0) {
		return "00";
	} else if (i > 255) {
		return "FF";
	}
	
	return "" + tohex[Math.floor(i/16)] + tohex[i%16];
}
function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		debug('its getting the movie name');
		return document[movieName];
	}
};

function getRandomPrefix(){
	var prefixArray = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p"];
	var rnd_num = Math.round(Math.random()*(prefixArray.length-1));
	var prefix = prefixArray[rnd_num];
	var tempPrefix = "#"+prefix+"_swfToTxt0";
	
	if($(tempPrefix).size() > 0){
		 return getRandomPrefix();
	}

	return prefix;
}



function cleanUptext(txtToClean){
	txtToClean = txtToClean.replace(/\"/g, "%22");
	txtToClean = txtToClean.replace(/&/, "%26");
	
	return txtToClean;
}

function generatetxtToSwId(){
	var d = new Date();
	var t = d.getTime();
	return t;
}

 function debug($obj) {
	if (window.console && window.console.log)
	  window.console.log($obj);
 };

  //
  // plugin defaults
  //
  $.fn.txtToSwf.defaults = {
	swf: 'txt.swf',
	swfLocation: 'flash/',
	version: '8'
  };
//
// end of closure
//
})(jQuery);